package day;

import java.util.LinkedList;
import java.util.List;

public class P482 {
    public String licenseKeyFormatting(String s, int k) {
        s = s.replace("-", "").toUpperCase();
        List<String> stringList = new LinkedList();
        int idx = s.length() - k;
        for (; idx >= 0; idx -= k) {
            stringList.add(0, s.substring(idx, idx + k));
        }
        if (idx < 0 && idx + k > 0) {
            stringList.add(0, s.substring(0, idx + k));
        }
        return String.join("-", stringList);
    }

    public static void main(String[] args) {
        System.out.println(new P482().licenseKeyFormatting("5F3Z-2e-9-w", 4));
    }
}
